<template>
  <div>
    <el-card class="ep-card ep-search" size="small">
      <el-form :model="searchForm" label-width="120px">
        <el-row>
          <el-col :span="7">
            <el-form-item label="指标类型编号" size="small">
              <el-input v-model="searchForm.setCode" placeholder="请输入编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="指标类型名称" prop="name" size="small">
              <el-input v-model="searchForm.setName" placeholder="请输入名称"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="small" @click="getList">查询</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" icon="el-icon-plus" size="small" @click="add">新增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="ep-card" style="height: calc(100vh - 280px);">
      <el-table :data="tableData" border :height="height" size="mini" style="width: 100%">
        <el-table-column type="index" label="序号" align="center" width="80">
        </el-table-column>
        <el-table-column prop="setCode" align="center" label="指标类型编号">
        </el-table-column>
        <el-table-column prop="setName" align="center" show-overflow-tooltip label="指标类型名称">
        </el-table-column>
        <el-table-column prop="isUse" align="center" show-overflow-tooltip label="启用状态" :formatter="isUseFor">
        </el-table-column>
        <el-table-column prop="remarks" align="center" show-overflow-tooltip label="备注">
        </el-table-column>
        <el-table-column prop="name" align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="addZbmx(scope.row)">指标明细</el-button>
            <el-button type="text" size="mini" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click="toDel(scope.row)" style="color: red;">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-card style="text-align:right;">
        <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
      </el-card>
    </el-card>
    <el-dialog :visible.sync="dialogTableVisible" :close-on-click-modal="false">
      <el-form :model="reportTemplate" ref="reportTemplate" label-width="120px">
        <!--基础表新增-->
        <el-form-item label="指标类型编号" size="small">
          <el-input v-model="reportTemplate.setCode" placeholder="请输入指标类型编号" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="指标类型名称" size="small">
          <el-input v-model="reportTemplate.setName" placeholder="请输入指标类型名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" size="small">
          <el-radio-group v-model="reportTemplate.isUse">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">不启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" size="small">
          <el-input type="textarea" v-model="reportTemplate.remarks"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleFormClose" size="small">取 消</el-button>
        <el-button type="primary" @click="doSave" size="small">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogTableVisible: false,
      reportTemplate: {
        id: "",
        setCode: "",
        setName: "",
        isUse: "",
        remarks: ""
      },
      searchForm: {
        setCode: "",
        setName: "",
        isUse: ""
      },
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      fileList: []
    };
  },
  computed: {
    height() {
      return `calc(100vh - 350px)`;
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    //table翻译 求和标识
    isUseFor: function(row, column) {
      return row.isUse == "1" ? "是" : "否";
    },
    //新增Z指标
    addZbmx(row) {
      console.log(row);
      this.$router.push({
        path: "/insert/set",
        query: {
          setId: row.id,
          setCode: row.setCode,
          setName: row.setName,
          isUse: row.isUse
        }
      });
    },
    //新增
    add() {
      this.reportTemplate.id = "";
      this.reportTemplate.setCode = "";
      this.reportTemplate.setName = "";
      this.reportTemplate.isUse = "1";
      this.reportTemplate.remarks = "";
      this.dialogTableVisible = true;
    },
    //保存方法
    doSave() {
      if (this.reportTemplate.setCode == "") {
        this.$message.error("请输入指标编号");
        return;
      }
      if (this.reportTemplate.setName == "") {
        this.$message.error("请输入指标名称");
        return;
      }

      let user = JSON.parse(sessionStorage.getItem("user"));
      let params = {};
      if (this.reportTemplate.id == "") {
        params = {
          setCode: this.reportTemplate.setCode,
          setName: this.reportTemplate.setName,
          isUse: this.reportTemplate.isUse,
          remarks: this.reportTemplate.remarks,
          createUserId: user.id,
          createUserName: user.realName,
          createTime: new Date()
        };
      } else {
        params = {
          id: this.reportTemplate.id,
          setCode: this.reportTemplate.setCode,
          setName: this.reportTemplate.setName,
          isUse: this.reportTemplate.isUse,
          remarks: this.reportTemplate.remarks
        };
      }
      this.$api
        .formPost("/api/biz/finIndicator/doSaveFinSet", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            if (this.reportTemplate.id == "") {
              this.$message({
                type: "success",
                message: "保存成功!"
              });
            } else {
              this.$message({
                type: "success",
                message: "编辑成功!"
              });
            }
            this.dialogTableVisible = false;
            this.getList();
          }
        });
    },

    //取消
    handleFormClose() {
      this.reportTemplate.id = "";
      this.reportTemplate.setCode = "";
      this.reportTemplate.setName = "";
      this.reportTemplate.isUse = "";
      this.reportTemplate.remarks = "";
      this.dialogTableVisible = false;
    },
    // 修改
    edit(row) {
      this.reportTemplate.id = row.id;
      this.reportTemplate.setCode = row.setCode;
      this.reportTemplate.setName = row.setName;
      this.reportTemplate.isUse = row.isUse;
      this.reportTemplate.remarks = row.remarks;
      this.dialogTableVisible = true;
    },

    // 获取条件列表
    getList() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        setCode: this.searchForm.setCode,
        setName: this.searchForm.setName,
        isUse: this.searchForm.isUse
      };
      this.$api
        .formPost("/api/biz/finIndicator/doGetList", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.tableData = res.retBody;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    //删除
    toDel(row) {
      let that = this;
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          that.del(row);
        })
        .catch(() => {});
    },
    del(row) {
      this.$api
        .formPost("/api/biz/finIndicator/doRmByFinSet", {
          id: row.id
        })
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.$message({
              type: "info",
              message: "删除成功"
            });
            this.getList();
          } else {
            this.$message({
              type: "info",
              message: res.retHead.message
            });
          }
        });
    },
    sizeChange(n) {
      this.pageSize = n;
      this.getList();
    },
    currentChange(n) {
      this.currentPage = n;
      this.getList();
    }
  }
};
</script>
